package com.hzlj.position.locate.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.core.date.DateUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.dict.annotation.DictConvert;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindSimpleQueryParamDTO;
import com.hzlj.position.config.config.PositionConfig;
import com.hzlj.position.config.service.JzJbxxDeviceBindService;
import com.hzlj.position.locate.common.dto.abnNoRecord.AbnNoRecordListDTO;
import com.hzlj.position.locate.common.dto.abnNoRecord.AbnNoRecordQueryParamDTO;
import com.hzlj.position.locate.common.dto.positionNewestEffective.PositionNewestEffectiveSimpleResultDTO;
import com.hzlj.position.locate.common.model.AbnNoRecord;
import com.hzlj.position.locate.dao.AbnNoRecordDao;
import com.hzlj.position.locate.service.AbnNoRecordService;
import com.hzlj.position.locate.service.PositionNewestEffectiveService;
import com.hzlj.position.locate.service.PositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;

/**
 * 定位分析-无记录表(AbnNoRecord)表服务接口
 *
 * @author lifh
 * @date 2023-03-28 19:26:08
 */
@Slf4j
@Service
public class AbnNoRecordServiceImpl implements AbnNoRecordService {
    @Resource
    private AbnNoRecordDao abnNoRecordDao;
    @Resource
    private PositionNewestEffectiveService positionNewestEffectiveService;
    @Resource
    private JzJbxxDeviceBindService jzJbxxDeviceBindService;
    @Resource
    private PositionConfig positionConfig;
    @Resource
    private PositionService positionService;

    @Override
    public void abn() {
        Date startTime = DateUtil.beginOfDay(DateUtil.yesterday());
        Date endTime = DateUtil.endOfDay(DateUtil.yesterday());

        List<String> positionJzIds = positionService.listJzIds4Effective(startTime, endTime);
        List<String> bindJzIds = jzJbxxDeviceBindService.listJzIds(new JzJbxxDeviceBindSimpleQueryParamDTO()
                .setMkh(!positionConfig.getAbn().getExcludeMkh())
                .setQzcs(!positionConfig.getAbn().getExcludeQzcs())
        );
        Collection<String> jzIds = CollectionUtil.subtract(bindJzIds, positionJzIds);
        this.saveAbnNoRecords(jzIds, startTime, endTime);
    }

    private void saveAbnNoRecords(Collection<String> jzIds, Date startTime, Date endTime) {
        if (ObjectUtil.isEmpty(jzIds)) {
            return;
        }
        Date endTimeHMS = DateUtils.parseDate(endTime, DateUtils.DATE_FULL_FORMAT);
        List<List<String>> jzIdsSplit = CollectionUtil.split(jzIds, 500);
        for (List<String> item : jzIdsSplit) {
            List<AbnNoRecord> adds = item.stream().map(jzId -> {
                PositionNewestEffectiveSimpleResultDTO position = positionNewestEffectiveService.getPositionNewestEffective(jzId);
                AbnNoRecord abn = new AbnNoRecord();
                abn.setId(POSITION_ID_GEN.nextIdStr());
                abn.setJzId(jzId);
                abn.setStartTime(startTime);
                abn.setEndTime(endTimeHMS);
                abn.setCreateTime(new Date());
                if (position != null) {
                    abn.setLastPosition(position.getPosition());
                    abn.setLastPositionId(position.getPositionId());
                    abn.setLastPositionTime(position.getPositionTime());
                }
                return abn;
            }).collect(Collectors.toList());
            abnNoRecordDao.saveBatch(adds);
        }
    }

    /**
     * 查询列表：分页
     *
     * @param dto  查询条件
     * @param page 分页信息
     * @return 对象列表
     */
    @Override
    @DictConvert
    public QueryPageResultDTO<AbnNoRecordListDTO> pageAbnNoRecord(AbnNoRecordQueryParamDTO dto, PageParamDTO page) {
        return this.abnNoRecordDao.pageAbnNoRecord(dto, page);
    }
}
